package com.xxx.server.controller;

import com.xxx.server.pojo.Admin;
import com.xxx.server.pojo.ChatMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;

/**
 *  Websocket
 * @ClassName WsController
 * @Descriotion TODO
 * @Author zyh
 * @Date 2021/4/26 10:25
 * @Version 1.0
 **/
@Controller
public class WsController {

    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;


    @MessageMapping("/ws/chat")
    public void handleMsg(Authentication authentication, ChatMsg chatMsg){

        //获取当前用户对象
        Admin admin = (Admin) authentication.getPrincipal();
        //登录的用户名
        chatMsg.setFrom(admin.getUsername());
        //昵称
        chatMsg.setFormNickName(admin.getName());
        //设置时间
        chatMsg.setDate(LocalDateTime.now());
        simpMessagingTemplate.convertAndSendToUser(chatMsg.getTo(),"/queue/chat",chatMsg);


    }
}
